package com.feiwo.nonego.rpc.reflect.bytebuddy;

import com.feiwo.nonego.rpc.reflect.api.ReflectInvoker;
import com.feiwo.nonego.rpc.spi.annotation.SPIClass;
import net.bytebuddy.ByteBuddy;

/**
 *
 *
 * @author feiwo 2025-08-17
 */
@SPIClass
public class BytebuddyReflectInvoker implements ReflectInvoker {
    @Override
    public Object invoke(Object serviceBean, Class<?> serviceClass, String methodName, Class<?>[] parameterTypes, Object[] parameters) throws Throwable {
        var impClass = new ByteBuddy()
                .subclass(serviceClass)
                .make()
                .load(BytebuddyReflectInvoker.class.getClassLoader())
                .getLoaded();
        var instance = impClass.getDeclaredConstructor().newInstance();
        var method = impClass.getMethod(methodName, parameterTypes);
        method.setAccessible(true);
        return method.invoke(instance, parameters);
    }
}
